﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Oracle.DataAccess.Client;
using Wicresoft.SelfService.DAL;
using Wicresoft.SelfService.DAO;
using System.Text;
namespace Wicresoft.SelfService.BL
{
    public class ITServiceHelper : IDisposable
    {
        private ITServiceDAL itService;
        private ITSMEventTypeDAL itsmEvent;
        private OSDAL osDAL;
        public ITServiceHelper()
        {
            osDAL = new OSDAL();
            itsmEvent = new ITSMEventTypeDAL();
            itService = new ITServiceDAL();
        }

        /// <summary>  
        /// 创建用户申请
        /// </summary>  
        /// <param name="model">申请信息</param>
        /// <returns>申请创建的状态，true为创建成功，false为失败</returns>  
        public bool CreateNewITService(ServiceDetail model)
        {
            QueryParameters[] sdParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.serviceRequestID},
                new QueryParameters(){ OracleDbName="flowInstanceId", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.flowInstanceId},
                new QueryParameters(){ OracleDbName="accountID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.accountID},
                new QueryParameters(){ OracleDbName="ServiceTypeID", OracleDbType=OracleDbType.Int32,OracleDbValue=model.serviceTypeID},
                new QueryParameters(){ OracleDbName="ComputerName", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.computerName},
                new QueryParameters(){ OracleDbName="IpAdress", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.ipAdress  },
                new QueryParameters(){ OracleDbName="OS", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.os},
                new QueryParameters(){ OracleDbName="MAC", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.mac},
                new QueryParameters(){ OracleDbName="Browser", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.browser},
                new QueryParameters(){ OracleDbName="Description", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=500, OracleDbValue=model.description},
                new QueryParameters(){ OracleDbName="CreateTime", OracleDbType=OracleDbType.Date, OracleDbValue=DateTime.Now},
                new QueryParameters(){ OracleDbName="Stauts", OracleDbType=OracleDbType.Int32, OracleDbValue=model.stauts},
                new QueryParameters(){ OracleDbName="Step", OracleDbType=OracleDbType.Int32, OracleDbValue=model.stepID},
                new QueryParameters(){ OracleDbName="CompanyIDs", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=100, OracleDbValue=model.companyIDs},
                new QueryParameters(){ OracleDbName="BreakDownLvl1", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl1},
                new QueryParameters(){ OracleDbName="BreakDownLvl2", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl2},
                new QueryParameters(){ OracleDbName="BreakDownLvl3", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl3},
                new QueryParameters(){ OracleDbName="BDDESCRIPTION", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=200, OracleDbValue=model.BDDescription},
                new QueryParameters(){ OracleDbName="BDSUMMARY", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=200, OracleDbValue=model.BDSummary},
                new QueryParameters(){ OracleDbName="APPSYSTEM", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.appName},
                new QueryParameters(){ OracleDbName="AdministrativeRegion", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=100, OracleDbValue=model.AdministrativeRegion}
            };
            int rowInfluenced = default(int);
            return itService.createServiceDetail(sdParam, "records_seq.nextval", out rowInfluenced);
        }
        /// <summary>  
        /// 更新服务申请信息
        /// </summary>  
        /// <param name="model">更新的服务申请信息</param>
        /// <returns>更新的状态，true为更新成功，false为失败</returns>  
        public bool UpdateITService(ServiceDetail model)
        {
            object stepid = null;
            if (model.stepID.HasValue) stepid = model.stepID.Value;
            QueryParameters[] sdParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.serviceRequestID.ToLower(), PredicateType=PredicateType.where, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="flowInstanceId", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.flowInstanceId, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="ServiceTypeID", OracleDbType=OracleDbType.Int32,OracleDbValue=model.serviceTypeID, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="ComputerName", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.computerName, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="IpAdress", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.ipAdress, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="OS", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.os, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="MAC", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.mac, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Browser", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=model.browser, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Description", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=500, OracleDbValue=model.description, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="UPDATETIME", OracleDbType=OracleDbType.Date, OracleDbValue=model.updateTime, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Stauts", OracleDbType=OracleDbType.Int32, OracleDbValue=model.stauts, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Step", OracleDbType=OracleDbType.Int32, OracleDbValue=stepid, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                //new QueryParameters(){ OracleDbName="CompanyIDs", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=100, OracleDbValue=model.companyIDs},
                new QueryParameters(){ OracleDbName="CompanyIDs", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=100, OracleDbValue=model.companyIDs, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="BreakDownLvl1", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl1, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="BreakDownLvl2", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl2, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="BreakDownLvl3", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.breakDownlvl3, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="BDDESCRIPTION", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=200, OracleDbValue=model.BDDescription, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="BDSUMMARY", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=200, OracleDbValue=model.BDSummary, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="APPSYSTEM", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=model.appName, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal} ,
                new QueryParameters(){ OracleDbName="AdministrativeRegion", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=100, OracleDbValue=model.AdministrativeRegion, PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal}          
            };
            int rowInfluenced = default(int);
            return itService.updateServiceDetail_By_QueryParams(sdParam, out rowInfluenced);
        }
        /// <summary>  
        /// 撤销IT服务申请
        /// </summary>  
        /// <param name="accountID">服务申请ID</param>
        /// <param name="accountID">新的状态</param>
        /// <returns>更新的状态，true为更新成功，false为失败</returns>  
        public bool UpdateITServiceStatus(string serviceRequestID, int status)
        {
            QueryParameters[] sdParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=serviceRequestID.ToLower(), PredicateType=PredicateType.where, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Stauts", OracleDbType=OracleDbType.Int32, OracleDbValue=status,PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal}         
            };
            int rowInfluenced = default(int);
            return itService.updateServiceDetail_By_QueryParams(sdParam, out rowInfluenced);
        }
        /// <summary>  
        /// 撤销IT服务申请
        /// </summary>  
        /// <param name="accountID">服务申请ID</param>
        /// <param name="accountID">新的状态</param>
        /// <param name="accountID">新的步骤</param>
        /// <returns>更新的状态，true为更新成功，false为失败</returns>  
        public bool UpdateITServiceStatus(string serviceRequestID, int status, int? step)
        {
            object stepid = null;
            if (step.HasValue) stepid = step.Value;
            QueryParameters[] sdParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=serviceRequestID.ToLower(), PredicateType=PredicateType.where, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Stauts", OracleDbType=OracleDbType.Int32, OracleDbValue=status,PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal},
                new QueryParameters(){ OracleDbName="Step", OracleDbType=OracleDbType.Int32, OracleDbValue=stepid,PredicateType=PredicateType.set, QueryOperator=QueryOperatorType.equal}
            };
            int rowInfluenced = default(int);
            return itService.updateServiceDetail_By_QueryParams(sdParam, out rowInfluenced);
        }
        /// <summary>  
        /// 创建附件信息
        /// </summary>  
        /// <param name="accountID">附件信息</param>
        /// <returns>创建状态，true为创建成功，false为失败</returns>  
        public bool CreateAttachment(Attachment att)
        {
            QueryParameters[] attParam = new QueryParameters[] {
                    new QueryParameters(){ OracleDbName="FileName", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=att.fileName},
                    new QueryParameters(){ OracleDbName="FileSize", OracleDbType=OracleDbType.Int32, OracleDbValue=att.fileSize},
                    new QueryParameters(){ OracleDbName="DateTime", OracleDbType=OracleDbType.Date,OracleDbValue=DateTime.Now},
                    new QueryParameters(){ OracleDbName="accountID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20,OracleDbValue=att.committerID},
                    new QueryParameters(){ OracleDbName="USERNAME", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20,OracleDbValue=att.committerUserName},
                    new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=att.serviceRequestID}
                    };
            int currVal = default(int);
            return itService.createAttacment_By_QueryParams(attParam, "", out currVal);
        }
        /// <summary>  
        /// 创建审批历史记录
        /// </summary>  
        /// <param name="history">审批历史记录</param>
        /// <returns>创建状态，true为创建成功，false为失败</returns>  
        public bool CreateServiceRecordHistory(ServiceHistory history)
        {
            QueryParameters[] attParam = new QueryParameters[] {
                    new QueryParameters(){ OracleDbName="SERVICEREQUESTID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=history.serviceRequestID},
                    new QueryParameters(){ OracleDbName="ACCOUNTID", OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20, OracleDbValue=history.approverID},
                    new QueryParameters(){ OracleDbName="USERNAME", OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20, OracleDbValue=history.approverName},
                    new QueryParameters(){ OracleDbName="ACTION", OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=history.action},
                    new QueryParameters(){ OracleDbName="DATETIME", OracleDbType=OracleDbType.Date,OracleDbValue=DateTime.Now},
                    new QueryParameters(){ OracleDbName="COMMENTS", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=500,OracleDbValue=history.comments}
                    };
            int currVal = default(int);
            return itService.createServiceHistory(attParam, out currVal);
        }
        /// <summary>  
        /// 通过IT服务申请单号获取审批历史信息
        /// </summary>  
        /// <param name="SERVICEREQUESTID">IT服务申请单号</param>
        /// <returns>审批历史信息</returns>  
        public List<ServiceHistory> getServiceRecordHistory_By_serviceRequestID(string serviceRequestID)
        {
            QueryParameters[] attParam = new QueryParameters[] {
                    new QueryParameters(){ OracleDbName="SERVICEREQUESTID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=serviceRequestID.ToLower(), PredicateType=PredicateType.where, QueryOperator=QueryOperatorType.equal}
                    };
            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "DATETIME", orderByType = orderByType.ascending } };
            return itService.getServiceHistory_By_QueryParams(attParam, param, null, null);
        }
        /// <summary>  
        /// 通过IT服务alias获取IT服务信息
        /// </summary>  
        /// <param name="ID">子模板名</param>
        /// <returns>IT服务信息</returns>  
        public ServiceList getServiceList_By_PartialName(string partialName)
        {
            QueryParameters[] slParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="PARTIALNAME", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=partialName.ToLower(), QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName="SERVICEID", OracleDbType=OracleDbType.Int32, OracleDbValue=null,QueryOperator=QueryOperatorType.isNull, PredicateType=PredicateType.where}
            };
            ServiceList serviceInfo = itService.getServiceList_By_QueryParams(slParam, null, null, null).FirstOrDefault();
            return serviceInfo;
        }

        /// <summary>  
        /// 通过IT服务alias获取IT子服务 
        /// </summary>  
        /// <param name="partialName">子模板名</param>
        /// <returns>IT子服务</returns>  
        public List<ServiceList> getServiceLists_By_PartialName(string partialName)
        {
            QueryParameters[] slParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="PARTIALNAME", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=50, OracleDbValue=partialName.ToLower(), QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName="SERVICEID", OracleDbType=OracleDbType.Int32, OracleDbValue=null,QueryOperator=QueryOperatorType.isNotNull, PredicateType=PredicateType.where}
            };
            List<ServiceList> serviceList = itService.getServiceList_By_QueryParams(slParam, null, null, null);
            return serviceList;
        }

        /// <summary>  
        /// 通过用户的地市，获取行政区 
        /// </summary>  
        /// <param name="company">地市</param>
        /// <returns>地市的行政区</returns>  
        public List<string> getAdministrativeByCompany(string company)
        {
            return itService.getAdministrativeByCompany(company);
        }

        /// <summary>  
        /// 通过ID来获取IT服务信息
        /// </summary>  
        /// <param name="ID">IT服务ID</param>
        /// <returns>IT服务信息</returns>  
        public ServiceList getServiceList_By_ID(int ID)
        {
            QueryParameters[] slParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="ID", OracleDbType=OracleDbType.Int32, OracleDbValue=ID,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            ServiceList serviceInfo = itService.getServiceList_By_QueryParams(slParam, null, null, null).FirstOrDefault();
            return serviceInfo;
        }

        /// <summary>  
        /// 通过ID来获取IT服务申请信息
        /// </summary>  
        /// <param name="ID">IT服务ID</param>
        /// <returns>IT服务申请信息</returns>  
        public ServiceDetail getServiceRequest_By_ID(string ID)
        {
            QueryParameters[] slParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=ID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            ServiceDetail serviceRequest = itService.getServiceDetail_By_QueryParams(slParam, null, null, null).FirstOrDefault();
            if (serviceRequest != null)
            {
                if (string.IsNullOrEmpty(serviceRequest.approveOwner))
                {
                    List<ServiceHistory> history = this.getServiceRecordHistory_By_serviceRequestID(ID);
                    if (history != null && history.Count > 0)
                    {
                        serviceRequest.LastProcessTime = history.Last().approveDate;
                        serviceRequest.LastProcessor = history.Last().approverName;
                    }
                }
                else
                {
                    serviceRequest.LastProcessTime = serviceRequest.updateTime;
                    serviceRequest.LastProcessor = serviceRequest.approveOwner;
                }
            }
            return serviceRequest;
        }

        /// <summary>  
        /// 通过IT服务ID来获取IT子服务 
        /// </summary>  
        /// <param name="ID">IT服务ID</param>
        /// <returns>IT子服务</returns>  
        public List<ServiceList> getServiceLists_By_ParentID(int ID)
        {
            QueryParameters[] slParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="SERVICEID", OracleDbType=OracleDbType.Int32, OracleDbValue=ID,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };
            List<ServiceList> serviceList = itService.getServiceList_By_QueryParams(slParam, null, null, null);
            return serviceList;
        }
        /// <summary>  
        ///  
        /// </summary>  
        /// <param name=""></param>
        /// <returns></returns>  
        public bool delAttachment_By_ServiceRequestIDAndFileName(string ServiceRequestID, string FileName)
        {
            QueryParameters[] attParam = new QueryParameters[]{
                new QueryParameters(){OracleDbName="SERVICEREQUESTID",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=ServiceRequestID.ToLower(),QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where},
                new QueryParameters(){OracleDbName="FILENAME",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=50,OracleDbValue=FileName.ToLower(),QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where}
            };
            int currVal = default(int);
            return itService.deleteAttacment_By_QueryParams(attParam, out currVal);
        }
        public List<string> getOS()
        {
            List<string> OSystems = new List<string>();
            string osStr = ConfigurationManager.AppSettings["OS"];
            string[] os = osStr.Split('|');
            OSystems = os.ToList();
            return OSystems;
        }

        public List<string> getOSFromDB()
        {
            
            return itService.getAllOS();

        }

        /// <summary>  
        /// 获取IT服务申请的全部附件信息
        /// </summary>  
        /// <param name="ID">IT服务申请ID</param>
        public List<Attachment> getAttachments_By_ServiceRequestID(string ID)
        {
            QueryParameters[] srParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=ID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
            };

            OrderbyParameters[] param = new OrderbyParameters[] { new OrderbyParameters() { orderByCol = "DATETIME", orderByType = orderByType.descending } };
            List<Attachment> atts = itService.getAttachment_By_QueryParams(srParam, param, null, null);
            return atts;
        }

        /// <summary>  
        /// 通过IT服务申请获取当前审批人
        /// </summary>  
        /// <param name="ID">IT服务申请ID</param>
        /// <returns>当前审批用户信息</returns>  
        public List<UserInfo> getRemoteApproverInfo_By_ServiceRequestID(string ID)
        {
            QueryParameters[] srParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=ID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName="Step", OracleDbType=OracleDbType.Int32, OracleDbValue=3,QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
                };

            List<UserInfo> uis = itService.getUserInfo_By_ITServiceRecord(srParam);
            return uis;
        }
        /// <summary>  
        /// 通过IT服务申请获取当前审批人
        /// </summary>  
        /// <param name="ID">IT服务申请ID</param>
        /// <param name="currentCompany">当前审批的公司名</param>
        /// <returns>当前审批用户信息</returns>  
        public List<UserInfo> getCurrentApproverInfo_By_ServiceRequestID(string ID, string currentCompany)
        {
            QueryParameters[] srParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2, OracleDbSize=20, OracleDbValue=ID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){ OracleDbName = "company", OracleDbType = OracleDbType.NVarchar2, OracleDbSize = 50, OracleDbValue = currentCompany, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where}
                };

            List<UserInfo> uis = itService.getUserInfo_By_ITServiceRecord(srParam);
            return uis;
        }

        /// <summary>  
        /// 获取所有审批人信息
        /// </summary>
        /// <param name="ID">IT服务申请ID</param>
        /// <param name="companyids">所有的审批公司数组</param>
        /// <returns>审批人信息</returns>  
        public List<UserInfo> getAllApproverInfo(int step, string[] companyids)
        {
            QueryParameters[] srParam = new QueryParameters[] {
                new QueryParameters(){ OracleDbName="s.ID", OracleDbType=OracleDbType.Int32,OracleDbValue=step,QueryOperator=QueryOperatorType.notBigger, PredicateType=PredicateType.where}
                };

            List<UserInfo> uis = itService.getUserInfo_By_Stepid(srParam);
            List<UserInfo> result = new List<UserInfo>();
            foreach (UserInfo ui in uis)
            {
                if (companyids.Contains(ui.company)) result.Add(ui);
            }
            return result;
        }
        public List<UserInfo> getUserInfoByAccountID(string accountID)
        {
            QueryParameters[] UIParam = new QueryParameters[]
            {
                new QueryParameters(){OracleDbName="ACCOUNTID",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=accountID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
             
            };
            List<UserInfo> UIList = itService.getUserInfo_By_QueryParams(UIParam, null, null, null);
            return UIList;
        }

        public UserInfo getUserInfo_By_AccountID(string accountID)
        {
            QueryParameters[] UIParam = new QueryParameters[]
            {
                new QueryParameters(){OracleDbName="ACCOUNTID",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=accountID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}
             
            };
            UserInfo user = itService.getUserInfo_By_QueryParams(UIParam, null, null, null).FirstOrDefault();
            return user;
        }

        /// <summary>  
        /// 获取主任信息
        /// </summary>
        /// <param name="serviceRequestID">IT服务申请单号</param>
        /// <param name="accountID">用户账号</param>
        /// <param name="directorRoles">主任角色数组</param>
        /// <returns>审批人信息</returns>  
        public bool getDirectorInfo_By_AccountID_And_DirectorRoles(string serviceRequestID, string accountID, string[] directorRoles)
        {
            QueryParameters[] UIParam = new QueryParameters[]
            {
                new QueryParameters(){ OracleDbName="serviceRequestID", OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=serviceRequestID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where},
                new QueryParameters(){OracleDbName="ACCOUNTID",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=accountID.ToLower(),QueryOperator=QueryOperatorType.equal, PredicateType=PredicateType.where}             
            };
            List<UserInfo> users = itService.getUserInfo_By_ITServiceRecord(UIParam);
            foreach (UserInfo user in users)
            {
                if (directorRoles.Contains(user.roleName)) return true;
            }
            return false;
        }
        /// <summary>  
        /// 通过参数获取用户角色配置信息
        /// </summary>
        public List<RolesConfig> getUserRolesConfigBy(string accountID, string userName, int? roleID, int isAdmin, int ps, int pi, out int counts, int company)
        {
            //QueryParameters[] RCParam = new QueryParameters[]
            //{
            //    new QueryParameters(){OracleDbName="accountID",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=accountID,QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where},
            //    new QueryParameters(){OracleDbName="userName",OracleDbType=OracleDbType.NVarchar2,OracleDbSize=20,OracleDbValue=userName,QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where},
            //    new QueryParameters(){OracleDbName="roleID",OracleDbType=OracleDbType.Int16,OracleDbValue=roleID,QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where},
            //    new QueryParameters(){OracleDbName="isAdmin",OracleDbType=OracleDbType.Int16,OracleDbValue=isAdmin,QueryOperator=QueryOperatorType.equal,PredicateType=PredicateType.where}
            //};
            List<QueryParameters> paraList = new List<QueryParameters>();
            if (!string.IsNullOrEmpty(accountID))
            {
                paraList.Add(new QueryParameters() { OracleDbName = "accountID", OracleDbType = OracleDbType.NVarchar2, OracleDbSize = 20, OracleDbValue = accountID, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where });
            }
            if (!string.IsNullOrEmpty(userName))
            {
                paraList.Add(new QueryParameters() { OracleDbName = "userName", OracleDbType = OracleDbType.NVarchar2, OracleDbSize = 20, OracleDbValue = userName, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where });
            }
            if (roleID != null)
            {
                paraList.Add(new QueryParameters() { OracleDbName = "roleID", OracleDbType = OracleDbType.Int16, OracleDbValue = roleID, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where });
            }
            if (isAdmin == 0 || isAdmin == 1)
            {
                paraList.Add(new QueryParameters() { OracleDbName = "isAdmin", OracleDbType = OracleDbType.Int16, OracleDbValue = isAdmin, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where });
            }
            if (company != 0)
            {
                paraList.Add(new QueryParameters() { OracleDbName = "company", OracleDbType = OracleDbType.Int16, OracleDbValue = company, QueryOperator = QueryOperatorType.equal, PredicateType = PredicateType.where });
            }
            List<RolesConfig> RCList = itService.getRolesInfoBy(paraList.ToArray(), ps, pi, out counts);
            return RCList;
        }

        #region //新增方法 ServiceList 
        public List<ClassifyConfig> getClassifyInfo()
        {
            return itService.getClassify();
        }

        public ClassifyConfig getClassifyByID(int ID) 
        {
            return itService.getClassifyByID(ID);
        }

        public bool updateClassify(ClassifyConfig classify)
        {
           return  itService.updateClassify(classify);
        }

        public bool deleteClassify(ClassifyConfig classify) 
        {
            return itService.deleteClassify(classify.ID);
        }

        public bool createClassify(ClassifyConfig classify) 
        {
            return itService.createClassify(classify);
        }

        public bool searchByServiceName(ClassifyConfig classify)
        {
            return itService.searchByServiceName(classify);
        
        }

        #endregion

        #region //新增方法 Breakdown
        public Dictionary<int , List<BreakConfig>> getBreakMenuData()
        {
            Dictionary<int, List<BreakConfig>> dic = new Dictionary<int, List<BreakConfig>>();
            List<BreakConfig> first=itService.getAllFirstLvlBreakInfo();
            List<BreakConfig> second = itService.getAllSecondLvlBreakInfo();
            foreach(BreakConfig b in first )
            {
                if (b.Parentid == 0)
                { 
                    dic.Add(b.ID,new List<BreakConfig>());
                    dic[b.ID].Add(b);
                }
            }
            foreach(BreakConfig b in second)
            {
                dic[b.Parentid].Add(b);
            }

            return dic;
        }

        public List<BreakConfig> getBreakDetail(int ID)
        {
            List<BreakConfig> breaks = new List<BreakConfig>();
            breaks.Add(itService.getFirstLvlBreakInfo(ID));
            breaks.Add(itService.getSecondLvlBreakInfo(ID));
            return breaks;
        }

        public BreakDowmModel GetBreakDownsToOne(int id)
        {
            var m1 = itService.getFirstLvlBreakInfo(id);
            var m2 = itService.getSecondLvlBreakInfo(id);
            BreakDowmModel model = new BreakDowmModel()
            {
                Tier1ID = m1.ID,
                Tier1 = m1.LvlName,
                Tier2ID = m2.ID,
                Tier2 = m2.LvlName
            };
            return model;
        }

        public BreakConfig getBreakDwonbyName(string lvlname)
        {
            return itService.getFirstLvlBreakInfoByName(lvlname);
        }

        public bool updateBreak(BreakDowmModel model)
        {
            var tm1 = itService.getFirstLvlBreakInfoByName(model.Tier1);
            if (tm1 != null)
            {
                bool flag1 = itService.updateBreak(model.Tier2ID, model.Tier2, tm1.ID);
                if (flag1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
                return false;

        }

        public List<BreakConfig> getAllFirstLvlBreakInfo() 
        {
            return itService.getAllFirstLvlBreakInfo();
        }

        public bool deletechild(int id) 
        {
            bool flag1 = itService.deleteBreak(id);
            if (flag1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool deleteBreak(int id,int parentid) 
        {
            bool flag1 = itService.deleteBreak(id);
            bool flag2 = itService.deleteBreak(parentid);
            if (flag1 && flag2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int createBreakdwon(BreakConfig model,int order)
        {
            return itService.createBreakdwon(model,order);
        }

        public bool searchByAppLvl1(string applvl1)
        {
            return itService.searchByApplvl1(applvl1);
        }

        public bool searchByAppLvl2(int parentid, string applvl2)
        {
            return itService.searchByAppLvl2(parentid,applvl2);
        }

        #endregion

        #region //OS Maintain

        public List<OS> getOSList()
        {
            return osDAL.GetAllOS();
        }

        public OS getOSByID(int id)
        {
            return osDAL.GetOSByID(id);
        }

        public bool updateOS(OS model)
        {
            return osDAL.Update(model);
        }

        public bool deleteOS(OS model)
        {
            return osDAL.Delete(model.ID);
        }

        public bool createOS(OS model)
        {
            return osDAL.Create(model);
        }

        public bool searchByOSName(OS model)
        {
            return osDAL.IsExist(model.OSName);
        }


        #endregion

        #region //Get ITSM Data(EventType)

        public List<EventType> getServiceEventTypeList()
        {
            return itsmEvent.getAllServiceEventType();
        }

        public List<EventType> getBreakEventTypeList()
        {
            return itsmEvent.getAllBreakEventType();
        }

        #endregion

        /// <summary>  
        /// 通过accountID参数获取单条用户角色配置信息
        /// </summary>
        public RolesConfig getSingleRoleConfigByAccountID(string accountID)
        {
            return itService.getSingleRoleInfoBy(accountID);
        }

        public List<ServiceList> getAllServiceList()
        {
            List<ServiceList> list = new List<ServiceList>();
            list = itService.getAllServiceList();
            return list;
        }
        public int updateRoleInfoBy(string accountID, bool isAdmin, int roleID)
        {
            return itService.updateRoleInfoBy(accountID, isAdmin, roleID);
        }
        public int insertRoleInfoBy(string accountID, bool isAdmin, int roleID)
        {
            return itService.insertRoleInfoBy(accountID, isAdmin, roleID);
        }
        public string getCurrentProcessor_By_RequestID(string serviceRequestID)
        {
            return itService.getCurrentProcessor_By_RequestID(serviceRequestID);
        }
        public ApproveSteps getApproveSteps_By_ID(int? id)
        {
            return itService.getApproveSteps_By_ID(id);
        }
        //public ApproveRole getApproveRole_By_roleName(string roleName)
        //{
        //    return itService.getApproveRole_By_roleName(roleName);
        //}
        //public UserRoleRelationship getUserRoleRelationship_By_ID(int? id)
        //{
        //    return itService.getUserRoleRelationship_By_ID(id);
        //}

        public int UpdateServiceRecords(string requestID, string status, string approver, string incidentNumber)
        {
            return itService.UpdateServiceRecords(requestID, status, approver, incidentNumber);
        }
        public string getServiceRecordNo()
        {
            return itService.createRecordNumberID();
        }

        //
        public bool UpdateUserAccountInfo(AccountInfo ai)
        {
            return itService.updateUerInfo(ai);
        }

        public bool UpdateUserAccountInfoToITPortal(AccountInfo ai)
        {
            return itService.updateUerInfoToPortal(ai);
        }

        public void commitTransaction()
        {
            itService.commitTransaction();
        }
        public void rollbackTransaction()
        {
            itService.rollbackTransaction();
        }
        public void closeDBConnection()
        {
            itService.closeDBConnection();
        }

        public void Dispose()
        {
            if (this.itService != null)
                this.itService.Dispose();
        }
    }
}